package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.db.DBConn;
import dao.ManagerMagDao;
import domain.Manager;

/**
 * 
 * May 9, 2009
 *
 * @author Fabian
 *
 */
public class ManagerMagImpl implements ManagerMagDao {

	/* (non-Javadoc)
	 * @see dao.ManagerMagDao#getManager(java.lang.String, java.lang.String)
	 */
	
	public Manager getManager(String nkname, String pwd) {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from t_admin where nickname = ? and password = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, nkname);
			pstmt.setString(2, pwd);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				Manager m = new Manager();
				m.setId(rs.getInt("id"));
				m.setNickname(rs.getString("nickname"));
				m.setPassword(rs.getString("password"));
				m.setName(rs.getString("name"));
				m.setSex(rs.getString("sex"));
				return m;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see dao.ManagerMagDao#getManager(int)
	 */
	
	public Manager getManager(int id) {
		Manager mgr = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from t_admin where id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				mgr = new Manager();
				mgr.setId(rs.getInt("id"));
				mgr.setNickname(rs.getString("nickname"));
				mgr.setPassword(rs.getString("password"));
				mgr.setName(rs.getString("name"));
				mgr.setSex(rs.getString("sex"));
				return mgr;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return mgr;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return mgr;
	}

	/* (non-Javadoc)
	 * @see dao.ManagerMagDao#modifyManager(vo.Manager)
	 */
	
	public boolean modifyManager(Manager mgr) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "update t_admin set name=?, sex=?, nickname=?, password=? where id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, mgr.getName());
			pstmt.setString(2, mgr.getSex());
			pstmt.setString(3, mgr.getNickname());
			pstmt.setString(4, mgr.getPassword());
			pstmt.setInt(5, mgr.getId());
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

}
